<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="设备类型">
          <el-select
            style="width: 100%"
            v-model="searchParam.relationId"
            placeholder="请选择设备类型"
            clearable
          >
            <el-option
              v-for="item in equipTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警等级">
          <el-select
            v-model="searchParam.alarmLevel"
            placeholder="请选择报警等级"
            clearable
          >
            <el-option
              v-for="item in useDictionary('ScaAlarmLevel')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警名称">
          <el-input
            v-model="searchParam.alarmName"
            placeholder="请输入报警名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="报警编号"
          prop="alarmCode"
          min-width="120"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="报警名称"
          prop="alarmName"
          min-width="120"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="报警等级"
          prop="alarmLevel"
          min-width="120"
          align="center"
          sortable
        >
          <template #default="scope">
            {{ useDictionary("ScaAlarmLevel", scope.row.alarmLevel) }}
          </template>
        </el-table-column>
        <el-table-column
          label="设备类型"
          prop="alarmLevel"
          min-width="120"
          align="center"
          sortable
        >
          <template #default="scope">
            {{ scope.row.equipTypeName }}
          </template>
        </el-table-column>
        <el-table-column
          label="持续时间(秒)"
          prop="sustainedTime"
          min-width="140"
          align="center"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="时间间隔(秒)"
          prop="intervalTime"
          min-width="140"
          align="center"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="报警内容"
          prop="alarmContent"
          min-width="100"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <scaalarmsetting-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : getTableList())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "IOTSCAALARMSETTING",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/iotadmin/scaalarmsetting/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  alarmCode: "",
  alarmName: "",
  alarmLevel: "",
  relationId: "",
};

const deleteParam = {
  api: "/iotadmin/scaalarmsetting/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/iotadmin/scaalarmsetting/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开报警配置表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改报警模板";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增报警模板";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开报警配置表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "报警模板详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除该条数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/iotadmin/scaalarmsetting/deleteItem/${id}`,
      method: "post",
    }).then((res) => {
      ElMessage.success("删除成功！");
      search();
    });
  });
}
// 获取设备类型下拉框
const equipTypeList: any = ref([]);
const getEquipTypeList = async () => {
  const res = await request({
    url: "/iotadmin/scaequiptype/getlist",
    method: "post",
    data: {},
  });
  if (res.code === 200) {
    equipTypeList.value = res.data;
    equipTypeList.value?.map((item) => {
      item.label = `${item.equipTypeName}`;
      item.value = `${item.id}`;
    });
  }
};

onMounted(() => {
  getEquipTypeList();
});
</script>
